package oa.struts.action;

import java.io.File;
import java.io.FileInputStream;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import oa.domain.system.Role;
import oa.domain.system.User;
import oa.service.UserService;
import oa.struts.action.base.BaseAction;
import oa.util.OAUtils;
import oa.util.UploadUtils;

@Controller("editInfoAction")
public class EditInfoAction extends BaseAction<User>{

	@Resource(name="userService")
	private UserService userService;
	
	private Long uid;
	
	private String resourceFileName;
	
    private File resource;  //存放上传的用户头像

	public String getResourceFileName() {
		return resourceFileName;
	}


	public void setResourceFileName(String resourceFileName) {
		this.resourceFileName = resourceFileName;
	}


	public Long getUid() {
		return uid;
	}


	public void setUid(Long uid) {
		this.uid = uid;
	}


	public File getResource() {
		return resource;
	}


	public void setResource(File resource) {
		this.resource = resource;
	}
	
	/**
	 * 编辑信息，上传用户图片
	 * @return
	 */
	public String editInfoUI()
	{
	    uid = OAUtils.getUserFromSession().getUid();
		User user = userService.getUserById(uid);
		String image = user.getImage();
		image = image.substring(image.lastIndexOf("\\")+1);   
		user.setImage(image);
		ActionContext.getContext().getValueStack().push(user);
		return "editInfoUI";
	}
	/**
	 * 上传用户图像
	 * @return
	 */
	public String editInfo()
	{
		User user = userService.getUserById(this.uid);
		String username = user.getUsername();
		System.out.println("fileName:"+resourceFileName);
		String imagePath = UploadUtils.saveUploadPic(resource,resourceFileName,username);
		if(!"error".equals(imagePath))
		{
			ActionContext.getContext().getValueStack().push("图片上传错误，请重新上传！");
		}
		user.setImage(imagePath);
		userService.updateUser(user);
		return "message";
	}

}
